"""
file: test_keyword.py
Author: 城下秋草
Desc: 关键字驱动测试脚本
"""
import os.path

import pytest
import yaml
from pom.pages.loginPage import LoginPage
from pom.util.util import get_yml_data
from pom.util.paths import case_dir
from pom.util.logger import logger


# def get_cases():
#     yml_file = r"C:\dev\qiucao\PlayWrightCourse\pom\cases\kdt.yml"
#     with open(yml_file,encoding="utf-8") as file:
#         return yaml.safe_load(file)

def run_step(page,step):
    login_page = LoginPage(page)
    if isinstance(step, str):
        func = "login_page." + step + "()"
        eval(func)
    elif isinstance(step, dict):
        for key,value in step.items():
            if key == "asserts":
                if isinstance(value,str):
                    func = "login_page." + key + "()"
                    eval(func)
                elif isinstance(value,dict):
                    for check,arg in value.items():
                        func = "login_page." + check + "(arg)"
                        eval(func)
            else:
                func = "login_page." + key + "(value)"
                eval(func)



def run_test(page,case):
    for step in case["steps"]:
        logger.debug(f"执行步骤：{step}")
        run_step(page,step)

test_cases = get_yml_data(os.path.join(case_dir,"kdt.yml"))["testcases"]
ids = [case["case_name"] for case in test_cases]

@pytest.mark.parametrize("case",test_cases,ids=ids)
def test_keyword(page,case,request):
    logger.info(f"------正在运行:{request.node.name}")
    run_test(page, case)